package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo13 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入老师的名字：");
        String name = scan.nextLine();
        try (Connection conn = DBUtil.getConnection();){
            String sql = "SELECT t.name,c.name,s.name\n" +
                         "FROM teacher t \n" +
                         "JOIN class c ON t.id = c.teacher_id\n" +
                         "JOIN student s ON c.id = s.class_id\n" +
                         "WHERE t.name = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String tName = rs.getString("t.name");
                String cName = rs.getString("c.name");
                String sName = rs.getString("s.name");
                System.out.println(tName+"  "+cName+"  "+sName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
